package com.tanhua.dubbo.server.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author: ymm
 * @Description: 类内容说明
 * @Date: Created in 14:10 2021/1/11
 * @ModifiedBy:
 */
@Service
public class IdService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //自增长id
    public Long createId(String type, String strId) {
        //全部转换为大写
        type = StringUtils.upperCase(type);

        //hash表中键是publishId,值是pid
        String idHashKey = "TANHUA_ID_HASH_" + type;

        if (this.redisTemplate.opsForHash().hasKey(idHashKey,strId)){
            return Long.valueOf(this.redisTemplate.opsForHash().get(idHashKey,strId).toString());
        }

        //hash表中没值,string表中是pid的增长值
        String idKey = "TANHUA_ID_" + type;
        Long id = this.redisTemplate.opsForValue().increment(idKey);

        this.redisTemplate.opsForHash().put(idHashKey,strId,id.toString());
        return id;
    }
}
